package com.hcbd.tdwt.ui.xingyu.activity;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.ui.xingyu.vo.MenuIconVO;
import com.hcbd.tdwt.view.MoveImgLinearLayout;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *  create zzh
 *  星宇菜单设置
 */
@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_menu_setting)
public class MenuSettingActivity extends BaseActivity {
    @ViewById
    RelativeLayout menuSettingLL;
    @ViewById
    View top_view1,top_view2,top_view3,top_view4,top_view5,top_view6,top_view7;
    @ViewById
    View bottom_view1,bottom_view2,bottom_view3,bottom_view4,bottom_view5,bottom_view6,bottom_view7,bottom_view8;
    @ViewById
    Button saveBtn;
    private Drawable frame_drawable;
    private Drawable line_drawable;
    private HashMap<Integer,View>hashMap;
    private List<MoveImgLinearLayout>moveImg;
    @SuppressLint("ClickableViewAccessibility")
    @AfterViews
    protected void init() {
        line_drawable=getResources().getDrawable(R.drawable.shape_dotted_line_bg);
        frame_drawable=getResources().getDrawable(R.drawable.view_frame_bg);
        hashMap=new HashMap<>();
        initIndexMap();
        currentView=top_view1;
        moveImg=new ArrayList<>();
        top_view1.post(this::initMenuChild);
    }
    public View currentView;
    public int currentIndex;
    void setLocation(int x, int y){
        if(x-top_view1.getX()<5&&y-top_view1.getY()<100){
            setViewBg(top_view1,1);
        }else if(x-top_view2.getX()<5&&y-top_view2.getY()<100){
            setViewBg(top_view2,2);
        }else if(x-top_view3.getX()<5&&y-top_view3.getY()<100){
            setViewBg(top_view3,3);
        }else if(x-top_view4.getX()<5&&y-top_view4.getY()<100){
            setViewBg(top_view4,4);
        }else if(x-top_view5.getX()<5&&y-top_view5.getY()<320){
            setViewBg(top_view5,5);
        }else if(x-top_view6.getX()<5&&y-top_view6.getY()<320){
            setViewBg(top_view6,6);
        }else if(x-top_view7.getX()<5&&y-top_view7.getY()<320){
            setViewBg(top_view7,7);
        }else if(x-bottom_view1.getX()<5&&y-bottom_view1.getY()<160){
            setViewBg(bottom_view1,9);
        } else if(x-bottom_view2.getX()<5&&y-bottom_view2.getY()<160){
            setViewBg(bottom_view2,10);
        }else if(x-bottom_view3.getX()<5&&y-bottom_view3.getY()<160){
            setViewBg(bottom_view3,11);
        }else if(x-bottom_view4.getX()<5&&y-bottom_view4.getY()<160){
            setViewBg(bottom_view4,12);
        }else if(x-bottom_view5.getX()<5&&y-bottom_view5.getY()<160){
            setViewBg(bottom_view5,13);
        }else if(x-bottom_view6.getX()<5&&y-bottom_view6.getY()<160){
            setViewBg(bottom_view6,14);
        } else if(x-bottom_view7.getX()<5&&y-bottom_view7.getY()<160){
            setViewBg(bottom_view7,15);
        } else if(x-bottom_view8.getX()<5&&y-bottom_view8.getY()<160){
            setViewBg(bottom_view8,16);
        } else{
            currentView.setBackground(frame_drawable);
        }
    }
    void setDown(MoveImgLinearLayout view){
        currentView.setBackground(frame_drawable);
        view.setX(currentView.getX());
        view.setY(currentView.getY());
        view.setIndex(currentIndex);
    }
    public void setViewBg(View newView,int index){
        currentIndex=index;
        currentView.setBackground(frame_drawable);
        newView.setBackground(line_drawable);
        currentView=newView;
    }
    @Click
    void saveBtn(){
        for(MoveImgLinearLayout menuVO:moveImg) {
            menuVO.insertMenu();
        }
        toast("保存成功!");
        setResult(RESULT_OK);
        finish();
    }
    @Click
    void headLeft(){
        finish();
    }

    public void initIndexMap(){
        hashMap.put(1,top_view1);
        hashMap.put(2,top_view2);
        hashMap.put(3,top_view3);
        hashMap.put(4,top_view4);
        hashMap.put(5,top_view5);
        hashMap.put(6,top_view6);
        hashMap.put(7,top_view7);
        hashMap.put(9,bottom_view1);
        hashMap.put(10,bottom_view2);
        hashMap.put(11,bottom_view3);
        hashMap.put(12,bottom_view4);
        hashMap.put(13,bottom_view5);
        hashMap.put(14,bottom_view6);
        hashMap.put(15,bottom_view7);
        hashMap.put(16,bottom_view8);
    }

    public void initMenuChild(){
        List<MenuIconVO>list= SQLite.select().from(MenuIconVO.class).queryList();
        for(int i=0;i<list.size();i++){
            MenuIconVO menuIconVO=list.get(i);
            View view=hashMap.get(menuIconVO.index);
            Drawable drawable=null;
            if("messageImg".equals(menuIconVO.iconType)){
                drawable=getResources().getDrawable(R.drawable.send_message);
            }else  if("contactImg".equals(menuIconVO.iconType)){
                drawable=getResources().getDrawable(R.drawable.new_contact);
            }else  if("bdEquipmentImg".equals(menuIconVO.iconType)){
                drawable=getResources().getDrawable(R.drawable.bd_equipment);
            }else  if("commandCenterImg".equals(menuIconVO.iconType)){
                drawable=getResources().getDrawable(R.drawable.command_center);
            }else  if("okImg".equals(menuIconVO.iconType)){
                drawable=getResources().getDrawable(R.drawable.home_ok);
            }else  if("locationUploadImg".equals(menuIconVO.iconType)){
                drawable=getResources().getDrawable(R.drawable.location_upload);
            }else  if("bdLocationImg".equals(menuIconVO.iconType)){
                drawable=getResources().getDrawable(R.drawable.bd_location);
            }
            MoveImgLinearLayout  moveImgLinearLayout =new MoveImgLinearLayout(this,drawable,view,menuIconVO);
            menuSettingLL.addView(moveImgLinearLayout);
            moveImgLinearLayout.setMoveInterface(new MoveImgLinearLayout.MoveInterface() {
                @Override
                public void move(float x, float y) {
                    setLocation((int)x,(int)y);
                }

                @Override
                public void up() {
                    setDown(moveImgLinearLayout);
                }
            });
            moveImg.add(moveImgLinearLayout);
        }
    }

}
